﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Data;

public partial class EditGaleryZoom : System.Web.UI.Page
{
    string[] PictureList = new string[0]; //[fotoFile.Length];
    public string pictureMap = ""; // Ar Databind šito variable var izmantot .ASPX lapā
    internal class CustomCompare : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return DateTime.Compare(
              Directory.GetCreationTime(y), Directory.GetCreationTime(x));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated == false)
        { Server.Transfer("login_aizliegts.aspx"); }
        else
        {

                ViewState["EditText"] = TextBox1.Text;
                ItemsGet();
   
        }
    }

    private void ItemsGet()
    {

        string bigPicName = Convert.ToString(String.Format(Request.QueryString["value"]));
      
        int MapNewN = 29;
       
        char[] burti = bigPicName.ToCharArray(0, 2);
        char c = burti[0];// pārgrupējam burtus
        char c2 = burti[1];
        burti[0] = c2;
        burti[1] = c;

        int MapesNr = Convert.ToInt32(Convert.ToString(burti[0]));
       MapNewN = Convert.ToInt32(Convert.ToString(burti[1])) * 10 + MapesNr;
        LinkButton2.PostBackUrl = "~/Galery.aspx?value=" + Convert.ToString(MapNewN);  //MapesNr);  

        string bigPicN = "";
        try
        {
            bigPicN = bigPicName.Substring(2);
        }
        catch (FormatException excQ)
        {
            bigPicN = bigPicName.Substring(1);
        }

       string  virtualPathDir = Server.MapPath("~/");

        /* NEW code */
        string virtualPathDir2 = Server.MapPath("~/");
        DataTable galery = new DataTable("galery");
        galery.ReadXml(virtualPathDir2 + "galery.xml");
        DataTable Mape = new DataTable("Mape");
        Mape.ReadXml(virtualPathDir2 + "Mape.xml");
        /* NEW code END */
        DataRow[] foundRows; // inicializē atrastās rindas.
        foundRows = galery.Select("MapID ="+ MapNewN.ToString()); // filter

        DataRow[] Maperow;
        Maperow = Mape.Select("ID=" + MapNewN.ToString()); // Filtrs.
        string MapName = Maperow[0].ItemArray[1].ToString().Substring(10); // noņem nost "galerijas\"
        
        int bildeStartNr = 0;
        Array.Resize(ref PictureList,foundRows.Length);
        for(int i=0;i<foundRows.Length;i++)
        {

            if (foundRows[i].ItemArray[2].ToString() == bigPicN)
            {
               bildeStartNr = i;
            }
            PictureList[i] = (string)foundRows[i][4];
        }
        

        int PicturCount = 0;
        string[] Komentāri = { " " };
        //Ielade XML failu kur ir bilžu komentāri
        XmlTextReader menuXml = new XmlTextReader(virtualPathDir + "\\BildesComentari.xml");
        XmlDocument MenuX = new XmlDocument();
        MenuX.Load(menuXml);
        XmlNode MenuBegin = MenuX.ChildNodes[2];

        if (Page.IsPostBack != true)
        {
            ViewState["bildesNR"] = bildeStartNr;
            foreach (XmlNode xmlM in MenuBegin.ChildNodes)
            {
                string Vards = xmlM.Attributes["vards"].Value;

                if (Vards == MapName)  // Mapes vārds.
                {
                    foreach (XmlNode xmlKoment in xmlM.ChildNodes)
                    {
                        Vards = xmlKoment.Attributes["vards"].Value;

                        if (Vards == bigPicN) // bildes vards
                        {
                            Array.Resize(ref Komentāri, Komentāri.Length + 1);
                            Komentāri[Komentāri.Length - 1] = xmlKoment.Attributes["Apraksts"].Value;
                            break;
                        }

                    }
                }
            }
        }
        else
        {
            int testg = (int)(ViewState["bildesNR"]);
            bildeStartNr = testg;
                // Te liek iekšā Komentāru bildei ja protams tāds ir 
                foreach (XmlNode xmlM in MenuBegin.ChildNodes)
                {
                    string Vards = xmlM.Attributes["vards"].Value;
                    if (Vards == MapName)
                    {
                        foreach (XmlNode xmlKoment in xmlM.ChildNodes)
                        {
                            Vards = xmlKoment.Attributes["vards"].Value;
                            if (Vards == (string)foundRows[testg][2])
                            {
                                Komentāri[Komentāri.Length - 1] = xmlKoment.Attributes["Apraksts"].Value;
                                break;
                            }
                        }
                    }
                }
            
        }

        menuXml.Close();
        ViewState["PicturListe"] = PictureList;
       
        lblCurrentPage.Text = "Bilde: " + Convert.ToString(((int)(ViewState["bildesNR"])) + 1)+ " no "
        + foundRows.Length.ToString();
        int ch = (int)(ViewState["bildesNR"]);
        if (Komentāri[0] != " ")
        {
            Repeater1.DataSource = Komentāri;
            Repeater1.DataBind();
        }
        //Label1.Text = Komentāri;
       TextBox1.Text = Komentāri[0];
        if (Page.IsPostBack == false)
        {
            pictureMap = PictureList[bildeStartNr];

            Page.DataBind();
        }
        else
        {
            pictureMap = PictureList[((int)(ViewState["bildesNR"]))];


            Page.DataBind();
        }

    }

    public int CurrentPage
    {
        get
        { // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default page index of 0
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }


    protected void cmdNext_Click1(object sender, EventArgs e)
    {
        int Nr = ((int)(ViewState["bildesNR"]));
        Nr = Nr + 1;
        if (Nr > PictureList.Length - 1)
        {
            Nr = 0;
        }
        ViewState["bildesNR"] = Nr;

        pictureMap = PictureList[Nr];
        Page.DataBind();

        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        ItemsGet();

    }
    protected void cmdPrev_Click(object sender, EventArgs e)
    {
        int Nr = ((int)(ViewState["bildesNR"]));
        Nr = Nr - 1;
        if (Nr < 0)
        {
            Nr = PictureList.Length - 1;
        }
        ViewState["bildesNR"] = Nr;

        pictureMap = PictureList[Nr];
        Page.DataBind();
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        ItemsGet();
    }


    protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
    {
        int Nr = ((int)(ViewState["bildesNR"]));

        if (Convert.ToInt32(e.PostBackValue) == 1)
        {
            Nr = Nr - 1;
            if (Nr < 0)
            {
                Nr = PictureList.Length - 1;
            }
            ViewState["bildesNR"] = Nr;
        }
        else if (Convert.ToInt32(e.PostBackValue) == 2)
        {
            Nr = Nr + 1;
            if (Nr > PictureList.Length - 1)
            {
                Nr = 0;
            }
            ViewState["bildesNR"] = Nr;
        }

        lblCurrentPage.Text = "Bilde: " + Convert.ToString(((int)(ViewState["bildesNR"]))) + " no "
        + PictureList.Length.ToString();

        pictureMap = PictureList[Nr];
        Page.DataBind();

    }


    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        // Šeit Updeito Komentāru vai arī Saglabā komentāru.
        string[] BilzuListe = (string[])ViewState["PicturListe"];
        int Nummurs = (int)ViewState["bildesNR"];
        string virtualPathDir = Server.MapPath("~/");

        XmlTextReader menuXml = new XmlTextReader(virtualPathDir + "\\BildesComentari.xml");
        XmlDocument MenuX = new XmlDocument();
        MenuX.Load(menuXml);
        menuXml.Close();
        XmlNode MenuBegin = MenuX.ChildNodes[2];
        string[] TextGabali = BilzuListe[Nummurs].Split('/');
        int i2 = 0;
        int MapesCount = MenuBegin.ChildNodes.Count;
        foreach (XmlNode xmlM in MenuBegin.ChildNodes)
        {// ie iet <image/> un meklē īsto <mapes/> elementu !
        int COunt = xmlM.ChildNodes.Count;
        int i = 0;
        i2++;
            string Vards = xmlM.Attributes["vards"].Value;
            if (Vards == TextGabali[1])
            { // Iet cauri <Mapes/> Child Elementiem <bilde/>
                foreach (XmlNode xmlKoment in xmlM.ChildNodes)
                {// Edit jau esošo <image/> xmlNode
                    i++;
                    Vards = xmlKoment.Attributes["vards"].Value;
                    if (Vards == TextGabali[2])
                    {// Esuatradis īsto NOde.
                        XmlAttribute xa = MenuX.CreateAttribute("vards");
                        xa.Value = TextGabali[2];
                        XmlAttribute xb = MenuX.CreateAttribute("Apraksts");
                        xb.Value = (string)ViewState["EditText"];
                        xmlKoment.RemoveAll(); //ReplaceChild(newNode, xmlKoment);
                        xmlKoment.Attributes.Append(xa);
                        xmlKoment.Attributes.Append(xb);
                        break;
                    }

                    if (COunt == i) // ja Bildei Nav neviena Komentāra tad uztaisa to :)
                    {
                        XmlNode newNode = MenuX.CreateNode(XmlNodeType.Element, "bilde", MenuX.NamespaceURI);
                        XmlAttribute xa = MenuX.CreateAttribute("vards");
                        xa.Value = TextGabali[2];
                        XmlAttribute xb = MenuX.CreateAttribute("Apraksts");
                        xb.Value = (string)ViewState["EditText"];
                        newNode.Attributes.Append(xa);
                        newNode.Attributes.Append(xb);
                        xmlM.AppendChild(newNode);
                        break;
                    }
                }
                break;
            }
            else
            {
                if (i2 == MapesCount)
                {// make NEw Mape Node
                    XmlNode newNode = MenuX.CreateNode(XmlNodeType.Element, "mape", MenuX.NamespaceURI);
                    XmlAttribute xa = MenuX.CreateAttribute("vards");
                    xa.Value = TextGabali[1];
                    newNode.Attributes.Append(xa);
                    MenuBegin.AppendChild(newNode);
                    // Make NEw bilde Node in Mape Node
                    XmlNode MapeJ = MenuBegin.LastChild;
                    XmlNode newNode2 = MenuX.CreateNode(XmlNodeType.Element, "bilde", MenuX.NamespaceURI);
                    XmlAttribute xa2 = MenuX.CreateAttribute("vards");
                    xa2.Value = TextGabali[2];
                    XmlAttribute xb = MenuX.CreateAttribute("Apraksts");
                    xb.Value = (string)ViewState["EditText"];
                    newNode2.Attributes.Append(xa2);
                    newNode2.Attributes.Append(xb);
                    MapeJ.AppendChild(newNode2);
                    break;
                }
            }
        }
        MenuX.Save(virtualPathDir + "\\BildesComentari.xml");
    }
}
